﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.ComponentModel;
using System.Data.SqlClient;

namespace WinLogicUniversity
{
    public partial class frmCategory : Form
    {
        LogicUniversityT2Entities entity = new LogicUniversityT2Entities();
        public frmCategory()
        {
            InitializeComponent();
        }

        #region Validation
        private Boolean MyValidation()
        {
            if (txtCategoryName.Text.Trim() == "")
            {
                errorProvider1.SetError(txtCategoryName, "Please Type Category Name");
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            BusinessController.CategoryDataAcess info = new BusinessController.CategoryDataAcess();
            DBBroker.CategoryBroker controller = new DBBroker.CategoryBroker();

            Boolean myvalidate;
            myvalidate = MyValidation();

            try
            {
                if (myvalidate == true)
                {
                    Category cat = new Category();

                    cat.CategoryID = txtCategoryID.Text;
                    cat.CategoryName = txtCategoryName.Text;
                    entity.AddToCategories(cat);
                    entity.SaveChanges();
                    MyMessageBox.ShowBox("Saved Successful!");
                    txtCategoryName.Text = "";
                    txtCategoryID.Text = DBBroker.CategoryBroker.AutoGenCategoryID();
                     
                }
            }
            catch (SqlException ex)
            {
                MyMessageBox.ShowBox("SQL Error " + ex.Number + ":" + ex.Message);
            }

        }

        private void frmCategory_Load(object sender, EventArgs e)
        {
            txtCategoryID.Text = DBBroker.CategoryBroker.AutoGenCategoryID();
        }

        private void frmCategory_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            frmManageItem frm = new frmManageItem();
            frm.ShowDialog();
        }
    }
}
